/*     */ package com.zimbra.cs.datasource.imap;
/*     */ 
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.mailclient.CommandFailedException;
/*     */ import com.zimbra.cs.mailclient.imap.CAtom;
/*     */ import com.zimbra.cs.mailclient.imap.CopyResult;
/*     */ import com.zimbra.cs.mailclient.imap.FetchResponseHandler;
/*     */ import com.zimbra.cs.mailclient.imap.Flags;
/*     */ import com.zimbra.cs.mailclient.imap.ImapConnection;
/*     */ import com.zimbra.cs.mailclient.imap.ImapData;
/*     */ import com.zimbra.cs.mailclient.imap.ImapRequest;
/*     */ import com.zimbra.cs.mailclient.imap.ImapResponse;
/*     */ import com.zimbra.cs.mailclient.imap.MailboxInfo;
/*     */ import com.zimbra.cs.mailclient.imap.MailboxName;
/*     */ import com.zimbra.cs.mailclient.imap.MessageData;
/*     */ import com.zimbra.cs.mailclient.imap.ResponseText;
/*     */ import java.io.IOException;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Collections;
/*     */ import java.util.Iterator;
/*     */ import java.util.List;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ final class RemoteFolder
/*     */ {
/*     */   private final ImapConnection connection;
/*     */   private String path;
/*     */   private int deleted;
/*  45 */   private static final Log LOG = ZimbraLog.datasource;
/*     */   
/*     */   RemoteFolder(ImapConnection connection, String path) {
/*  48 */     this.connection = connection;
/*  49 */     this.path = path;
/*     */   }
/*     */   
/*     */   public void create() throws IOException {
/*  53 */     info("creating folder", new Object[0]);
/*     */     try {
/*  55 */       this.connection.create(this.path);
/*     */     }
/*     */     catch (CommandFailedException e) {
/*  58 */       if (!exists()) throw e;
/*     */     }
/*     */   }
/*     */   
/*     */   public void delete() throws IOException {
/*  63 */     info("deleting folder", new Object[0]);
/*     */     try {
/*  65 */       this.connection.delete(this.path);
/*     */     }
/*     */     catch (CommandFailedException e) {
/*  68 */       if (exists()) throw e;
/*     */     }
/*     */   }
/*     */   
/*     */   public RemoteFolder renameTo(String newName) throws IOException {
/*  73 */     info("renaming folder to '%s'", new Object[] { newName });
/*  74 */     this.connection.rename(this.path, newName);
/*  75 */     return new RemoteFolder(this.connection, newName);
/*     */   }
/*     */   
/*     */   public CopyResult copyMessage(long uid, String mbox) throws IOException {
/*  79 */     assert (isSelected());
/*  80 */     String seq = String.valueOf(uid);
/*  81 */     ImapRequest req = this.connection.newUidRequest(CAtom.COPY, new Object[] { seq, new MailboxName(mbox) });
/*  82 */     ResponseText rt = req.sendCheckStatus().getResponseText();
/*  83 */     if (rt.getCCode() == CAtom.COPYUID) {
/*  84 */       CopyResult cr = (CopyResult)rt.getData();
/*     */       
/*  86 */       if ((cr != null) && (cr.getToUids()[0] != 0L)) {
/*  87 */         return cr;
/*     */       }
/*     */     }
/*  90 */     return null;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public void deleteMessages(List<Long> uids)
/*     */     throws IOException
/*     */   {
/* 100 */     assert (isSelected());
/* 101 */     int size = uids.size();
/* 102 */     debug("deleting %d messages(s) from folder", new Object[] { Integer.valueOf(size) });
/* 103 */     for (int i = 0; i < size; i += 16) {
/* 104 */       String seq = ImapData.asSequenceSet(uids.subList(i, i + Math.min(size - i, 16)));
/*     */       
/* 106 */       this.connection.uidStore(seq, "+FLAGS.SILENT", "(\\Deleted)");
/*     */       
/* 108 */       if (this.connection.hasUidPlus()) {
/* 109 */         this.connection.uidExpunge(seq);
/*     */       }
/*     */     }
/* 112 */     this.deleted += size;
/*     */   }
/*     */   
/*     */   public void deleteMessage(long uid) throws IOException {
/* 116 */     assert (isSelected());
/* 117 */     debug("deleting message with uid %d", new Object[] { Long.valueOf(uid) });
/* 118 */     String seq = String.valueOf(uid);
/* 119 */     this.connection.uidStore(seq, "+FLAGS.SILENT", "(\\Deleted)");
/*     */     
/* 121 */     if (this.connection.hasUidPlus()) {
/* 122 */       this.connection.uidExpunge(seq);
/*     */     }
/* 124 */     this.deleted += 1;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public void close()
/*     */     throws IOException
/*     */   {
/* 133 */     if ((this.deleted > 0) && (!this.connection.hasUidPlus())) {
/* 134 */       this.connection.close_mailbox();
/*     */     }
/*     */   }
/*     */   
/*     */   public List<Long> getUids(long startUid, long endUid) throws IOException {
/* 139 */     assert (isSelected());
/* 140 */     String end = endUid > 0L ? String.valueOf(endUid) : "*";
/* 141 */     List<Long> uids = this.connection.getUids(startUid + ":" + end);
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 146 */     if ((endUid <= 0L) && (uids.size() == 1) && (((Long)uids.get(0)).longValue() < startUid)) {
/* 147 */       return Collections.emptyList();
/*     */     }
/*     */     
/* 150 */     Iterator<Long> it = uids.iterator();
/* 151 */     while (it.hasNext()) {
/* 152 */       Long uid = (Long)it.next();
/* 153 */       if ((uid.longValue() < startUid) || ((endUid > 0L) && (uid.longValue() > endUid))) {
/* 154 */         LOG.warn("UID FETCH %d:%d returned UID out of range: %d", new Object[] { Long.valueOf(startUid), Long.valueOf(endUid), uid });
/* 155 */         it.remove();
/*     */       }
/*     */     }
/*     */     
/* 159 */     Collections.sort(uids, Collections.reverseOrder());
/* 160 */     return uids;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public List<MessageData> getFlags(long startUid, long endUid)
/*     */     throws IOException
/*     */   {
/* 169 */     final List<MessageData> mds = new ArrayList();
/* 170 */     String end = endUid > 0L ? String.valueOf(endUid) : "*";
/* 171 */     this.connection.uidFetch(startUid + ":" + end, "FLAGS", new FetchResponseHandler()
/*     */     {
/*     */       public void handleFetchResponse(MessageData md) {
/* 174 */         Flags flags = md.getFlags();
/* 175 */         if ((flags != null) && (!flags.isDeleted())) {
/* 176 */           mds.add(md);
/*     */         }
/*     */       }
/*     */     });
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 185 */     if ((endUid <= 0L) && (mds.size() == 1) && (((MessageData)mds.get(0)).getUid() < startUid)) {
/* 186 */       return Collections.emptyList();
/*     */     }
/* 188 */     return mds;
/*     */   }
/*     */   
/*     */   public boolean exists() throws IOException {
/* 192 */     return !this.connection.list("", this.path).isEmpty();
/*     */   }
/*     */   
/*     */   public MailboxInfo getMailboxInfo() {
/* 196 */     return this.connection.getMailboxInfo();
/*     */   }
/*     */   
/*     */   public MailboxInfo select() throws IOException {
/* 200 */     MailboxInfo mi = this.connection.select(this.path);
/*     */     
/* 202 */     if (mi.getUidValidity() <= 0L) {
/* 203 */       mi.setUidValidity(1L);
/*     */     }
/* 205 */     if (mi.getExists() == -1L) {
/* 206 */       debug("Server did not provide EXISTS", new Object[0]);
/* 207 */       mi.setExists(1);
/*     */     }
/* 209 */     return mi;
/*     */   }
/*     */   
/*     */   public MailboxInfo status() throws IOException {
/* 213 */     MailboxInfo mi = this.connection.status(this.path, new Object[] { "UIDVALIDITY", "UIDNEXT", "MESSAGES" });
/*     */     
/* 215 */     if (mi.getUidValidity() <= 0L) {
/* 216 */       mi.setUidValidity(1L);
/*     */     }
/* 218 */     if (!this.path.equals(mi.getName())) {
/* 219 */       this.path = mi.getName();
/*     */     }
/* 221 */     if (mi.getExists() == -1L) {
/* 222 */       debug("Server did not provide MESSAGES", new Object[0]);
/* 223 */       mi.setExists(1);
/*     */     }
/* 225 */     return mi;
/*     */   }
/*     */   
/*     */   public boolean isSelected() {
/* 229 */     MailboxInfo mb = this.connection.getMailboxInfo();
/* 230 */     return (mb != null) && (mb.getName().equals(this.path));
/*     */   }
/*     */   
/*     */   public String getPath() {
/* 234 */     return this.path;
/*     */   }
/*     */   
/*     */   public void debug(String fmt, Object... args) {
/* 238 */     if (LOG.isDebugEnabled()) {
/* 239 */       LOG.debug(errmsg(String.format(fmt, args)));
/*     */     }
/*     */   }
/*     */   
/*     */   public void info(String fmt, Object... args) {
/* 244 */     LOG.info(errmsg(String.format(fmt, args)));
/*     */   }
/*     */   
/*     */   public void info(String msg, Throwable e) {
/* 248 */     LOG.info(errmsg(msg), e);
/*     */   }
/*     */   
/*     */   public void warn(String msg, Throwable e) {
/* 252 */     LOG.error(errmsg(msg), e);
/*     */   }
/*     */   
/*     */   public void warn(String fmt, Object... args) {
/* 256 */     LOG.warn(errmsg(String.format(fmt, args)));
/*     */   }
/*     */   
/*     */   public void error(String msg, Throwable e) {
/* 260 */     LOG.error(errmsg(msg), e);
/*     */   }
/*     */   
/*     */   private String errmsg(String s) {
/* 264 */     return String.format("Remote folder '%s': %s", new Object[] { getPath(), s });
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/datasource/imap/RemoteFolder.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */